<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="dictType">
        <el-select @change="handleQuery" filterable placeholder="字典类型" clearable v-model="queryParams.dictType">
          <el-option :label="item.dictType" :value="item.dictType" v-for="item in dict_type_list" :key="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item prop="dictName">
        <el-select @change="handleQuery" filterable placeholder="字典名称" clearable v-model="queryParams.dictName">
          <el-option :label="item.dictName" :value="item.dictName" v-for="item in dict_name_list" :key="item.id" />
        </el-select>
      </el-form-item>

      <el-form-item prop="dictLabel">
        <el-input v-model="queryParams.dictLabel" placeholder="字典标签" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item prop="dictValue">
        <el-input v-model="queryParams.dictValue" placeholder="字典键值" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item prop="dictValueRef">
        <el-input
          v-model="queryParams.dictValueRef"
          placeholder="字典键值映射"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['distribution:dict:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['distribution:dict:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['distribution:dict:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['distribution:dict:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" ref="TableRef" :data="dictList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="字典编码" align="center" prop="dictCode" v-if="true" />
      <el-table-column label="字典类型" align="center" prop="dictType" />
      <el-table-column label="字典名称" align="center" prop="dictName" />
      <el-table-column label="字典排序" align="center" prop="dictSort" />
      <el-table-column label="字典标签" align="center" prop="dictLabel" />
      <el-table-column label="字典键值" align="center" prop="dictValue" />
      <el-table-column label="字典键值映射" align="center" prop="dictValueRef" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column :label="$t('备注')" align="center" prop="remark" />
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['distribution:dict:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['distribution:dict:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改ERP系统字典数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="字典类型" prop="dictType">
          <el-input style="width: 100%" v-model="form.dictType" placeholder="请输入字典类型" />
          <!-- <el-select style="width: 100%;" v-model="form.dictType">
            <el-option label="xxxx" value="xxxx"></el-option>
            <el-option label="yyyy" value="yyyy"></el-option>
          </el-select> -->
        </el-form-item>
        <el-form-item label="字典名称" prop="dictName">
          <el-input v-model="form.dictName" placeholder="请输入字典名称" />
        </el-form-item>
        <el-form-item label="字典排序" prop="dictSort">
          <el-input v-model="form.dictSort" placeholder="请输入字典排序" />
        </el-form-item>
        <el-form-item label="字典标签" prop="dictLabel">
          <el-input v-model="form.dictLabel" placeholder="请输入字典标签" />
        </el-form-item>
        <el-form-item label="字典键值" prop="dictValue">
          <el-input v-model="form.dictValue" placeholder="请输入字典键值" />
        </el-form-item>
        <el-form-item label="字典键值映射" prop="dictValueRef">
          <el-input v-model="form.dictValueRef" placeholder="请输入字典键值映射" />
        </el-form-item>
        <el-form-item :label="$t('备注')" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getDictListByGroup } from '@/api/public'

import { listDict, getDict, delDict, addDict, updateDict } from '@/api/platform/dict'
import { useDict } from '@/utils/useDict'

export default {
  activated() {
    this.$nextTick(() => this.$refs['TableRef']?.doLayout())
  },
  name: 'platformDict',
  data() {
    return {
      /** */
      dict_type_list: [],
      dict_name_list: [],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // ERP系统字典数据表格数据
      dictList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dictType: undefined,
        dictName: undefined,
        dictSort: undefined,
        dictLabel: undefined,
        dictValue: undefined,
        dictValueRef: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dictCode: [{ required: true, message: '字典编码不能为空', trigger: 'blur' }],
        dictType: [{ required: true, message: '字典类型不能为空', trigger: 'change' }],
        dictName: [{ required: true, message: '字典名称不能为空', trigger: 'blur' }],
        dictSort: [{ required: true, message: '字典排序不能为空', trigger: 'blur' }],
        dictLabel: [{ required: true, message: '字典标签不能为空', trigger: 'blur' }],
        dictValue: [{ required: true, message: '字典键值不能为空', trigger: 'blur' }],
        // dictValueRef: [{ required: true, message: '字典键值映射不能为空', trigger: 'blur' }],
        status: [{ required: true, message: '状态不能为空', trigger: 'change' }],
        // remark: [{ required: true, message: '备注不能为空', trigger: 'blur' }],
      },
    }
  },
  created() {
    this.getList()
    this.getDicts()
  },
  methods: {
    getDicts() {
      getDictListByGroup('dict_type').then((res) => {
        this.dict_type_list = res.data
      })
      getDictListByGroup('dict_name').then((res) => {
        this.dict_name_list = res.data
      })
    },
    /** 查询ERP系统字典数据列表 */
    getList() {
      this.loading = true
      listDict(this.queryParams).then((response) => {
        this.dictList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        dictCode: undefined,
        dictType: undefined,
        dictName: undefined,
        dictSort: undefined,
        dictLabel: undefined,
        dictValue: undefined,
        dictValueRef: undefined,
        status: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryParams.pageSize = 10
      this.queryParams.pageNum = 1
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.dictCode)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加ERP系统字典数据'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true
      this.reset()
      const dictCode = row.dictCode || this.ids
      getDict(dictCode).then((response) => {
        this.loading = false
        this.form = response.data
        this.open = true
        this.title = '修改ERP系统字典数据'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true
          if (this.form.dictCode != null) {
            updateDict(this.form)
              .then((response) => {
                this.$modal.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
              .finally(() => {
                this.buttonLoading = false
              })
          } else {
            addDict(this.form)
              .then((response) => {
                this.$modal.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
              .finally(() => {
                this.buttonLoading = false
              })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dictCodes = row.dictCode || this.ids
      this.$modal
        .confirm('是否确认删除ERP系统字典数据编号为"' + dictCodes + '"的数据项？')
        .then(() => {
          this.loading = true
          return delDict(dictCodes)
        })
        .then(() => {
          this.loading = false
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'distribution/dict/export',
        {
          ...this.queryParams,
        },
        `dict_${new Date().getTime()}.xlsx`,
      )
    },
  },
}
</script>
